<?php
error_reporting(E_ERROR | E_PARSE);
require_once '../generales/lib/bd/conexionAdo5.php';
require_once '../generales/lib/adodb5/adodb.inc.php';
require_once '../generales/lib/yaml/Yaml.class.php';
//require_once("../../lib/adodb5/adodb.inc.php");
//require_once("../../lib/bd/conexionAdo5.php");
//require_once("../../lib/yaml/Yaml.class.php");

//print $_SERVER['DOCUMENT_ROOT'];
//print 'Jodas'; exit;
//
//session_name('cidesa');
//session_start();

//print dirname(__FILE__);exit;
  

  class bdAdo5
  {
    private $conn;
    private $bd;
    private $confbd;
    private $schema;
    private $empresa;

    function bdAdo5()
    {
      $this->bd=new conexionAdo5();
      $opciones = Yaml::load("../generales/lib/bd/databases.yml");
      $this->DataBase=$opciones['database']['name'];
      $confbd = $opciones['database']['name'];
      /*if(isset($_SESSION['schema'])) $this->schema = $_SESSION['schema'];
      else{
        if($_SERVER['REQUEST_METHOD']=='GET'){
          if(isset($_GET['schema'])){
            $_SESSION['schema'] = $_GET['schema'];
            $this->schema = $_GET['schema'];
          }else if(isset($_SESSION['schema'])) $this->schema = $_SESSION['schema'];
          else $this->schema=$opciones[$confbd]['schema'];
        }elseif($_SERVER['REQUEST_METHOD']=='POST'){
          if(isset($_POST['schema'])){
            $_SESSION['schema'] = $_POST['schema'];
            $this->schema = $_POST['schema'];
          }else if(isset($_SESSION['schema'])) $this->schema = $_SESSION['schema'];
          else $this->schema=$opciones[$confbd]['schema'];
        }
      }*/
      $hostname=$opciones[$confbd]['host'];
      $user=$opciones[$confbd]['usuario'];
      $password=$opciones[$confbd]['password'];
      $dbname=$opciones[$confbd]['bd'];
      //$this->schema=$opciones[$confbd]['schema'];
      $port=$opciones[$confbd]['port'];
      $this->empresa=$opciones[$confbd]['empresa'];
      $this->schema=$opciones[$confbd]['schema'];
      if(strtolower($this->DataBase)=='mysql'){
            $this->conn=$this->bd->conectar("mysql",$hostname,$user,$password,$port,$dbname);
      }
      else if(strtolower($this->DataBase)=='postgres'){
        $this->conn=$this->bd->conectar("postgres",$hostname,$user,$password,$port,$dbname);
      }
      //print "<pre>Empresa: ".$this->empresa." ,Schema: ".$this->schema.", Db: ".$dbname;exit;
      //Configuración de la codificación por defecto
      $this->conn->Execute('SET CLIENT_ENCODING TO "UNICODE"');
    }

    function select($sql, $schema = '')
    {
        if (strtolower($this->DataBase) == 'mysql') {
            $this->conn->SetFetchMode(ADODB_FETCH_ASSOC);
            $rs=$this->conn->Execute($sql);
        } else if (strtolower($this->DataBase) == 'postgres') {
            if($schema=='') $this->conn->Execute('SET search_path TO "'.$this->schema.'"');
            else $this->conn->Execute('SET search_path TO "'.strtoupper($schema).'"');
            $rs=$this->conn->Execute($sql);
        }
        //print "<pre>".'SET search_path TO "'.$this->schema.'"';exit;
        return $rs;
    }

    function selectu($sql)
    {
        if (strtolower($this->DataBase) == 'mysql') {
            $rs=$this->conn->Execute($sql);
        } else if (strtolower($this->DataBase) == 'postgres') {
            $this->conn->Execute('SET search_path TO "SIMA_USER"');
            $rs=$this->conn->Execute($sql);
        }
      return $rs;
    }

    function actualizar($sql)
    {
      $this->conn->Execute('SET search_path TO "'.$this->schema.'"');
      $this->conn->Execute($sql);
    }

    function closed()
    {
      $this->conn->Close();
    }

    function setAutoSql($table, $arrFields, $mode, $where=false, $forceUpdate=true,$magicq=false){
      $this->conn->AutoExecute($table, $arrFields, $mode, $where=false, $forceUpdate=true,$magicq=false);
      return true;
    }

    function getSchema()
    {return $this->schema;}

    function getEmpresa()
    {return $this->empresa;}

    function Validar()
    {
    	return true;
    }

  }
//$record["firstName"] = "Carol";
//$record["lasTname"] = "Smith";
//$conn->AutoExecute($table,$record,'INSERT');
//executes "INSERT INTO $table (firstName,lasTname) values ('Carol',Smith')";
//
//$record["firstName"] = "Carol";
//$record["lasTname"] = "Jones";
//$conn->AutoExecute($table,$record,'UPDATE', "lastname like 'Sm%'");
//# executes "UPDATE $table SET firstName='Carol',lasTname='Jones' WHERE lastname like 'Sm%'";
?>
